namespace TSL.DAL
{
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Text;
    using TSL.Model;

    public class caipiao
    {
        public void Addcaipiao(TSL.Model.caipiao model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("TSL_caipiao_ADD");
            db.AddInParameter(dbCommand, "sscname", DbType.AnsiString, model.sscname);
            db.AddInParameter(dbCommand, "url", DbType.AnsiString, model.url);
            db.AddInParameter(dbCommand, "picture", DbType.AnsiString, model.picture);
            db.AddInParameter(dbCommand, "memo", DbType.AnsiString, model.memo);
            db.AddInParameter(dbCommand, "propertys", DbType.AnsiString, model.propertys);
            db.AddInParameter(dbCommand, "state", DbType.AnsiString, model.state);
            db.AddInParameter(dbCommand, "orderid", DbType.AnsiString, model.orderid);
            db.AddInParameter(dbCommand, "picture", DbType.AnsiString, model.picture);
            db.AddInParameter(dbCommand, "memo", DbType.AnsiString, model.memo);
            db.ExecuteNonQuery(dbCommand);
        }

        public void DeleteCaipiao(string ID)
        {
            List<string> strList = new List<string>();
            strList.Add("delete from caipiao where ID = '" + ID + "'");
            DbHelperSQL.ExecuteSqlTran(strList);
        }

        public bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from caipiao");
            strSql.Append(" where sscname=@name ");
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@name", SqlDbType.VarChar, 50) };
            parameters[0].Value = name;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public List<TSL.Model.caipiao> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_CaiPiao ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("order by id asc");
            List<TSL.Model.caipiao> list = new List<TSL.Model.caipiao>();
            using (IDataReader dataReader = DatabaseFactory.CreateDatabase().ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(this.ReaderBind(dataReader));
                }
            }
            return list;
        }

        public DataSet GetListFromView(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_caipiao ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("order by  orderid asc");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public TSL.Model.caipiao GetModel(int sscid)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_caipiao_GetModel");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, sscid);
            TSL.Model.caipiao model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public TSL.Model.caipiao GetModel(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_caipiao_GetModelByID");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            TSL.Model.caipiao model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public TSL.Model.caipiao ReaderBind(IDataReader dataReader)
        {
            TSL.Model.caipiao model = new TSL.Model.caipiao();
            model.id = dataReader["id"].ToString();
            model.sscname = dataReader["sscname"].ToString();
            model.url = dataReader["url"].ToString();
            model.propertys = dataReader["propertys"].ToString();
            model.state = dataReader["state"].ToString();
            model.orderid = dataReader["orderid"].ToString();
            model.memo = dataReader["memo"].ToString();
            model.picture = dataReader["picture"].ToString();
            model.weishu = int.Parse(dataReader["weishu"].ToString());
            return model;
        }

        public void Update(TSL.Model.caipiao model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_caipiao_Update");
            db.AddInParameter(dbCommand, "id", DbType.AnsiString, model.id);
            db.AddInParameter(dbCommand, "sscname", DbType.AnsiString, model.sscname);
            db.AddInParameter(dbCommand, "url", DbType.AnsiString, model.url);
            db.AddInParameter(dbCommand, "propertys", DbType.AnsiString, model.propertys);
            db.AddInParameter(dbCommand, "state", DbType.AnsiString, model.state);
            db.AddInParameter(dbCommand, "orderid", DbType.AnsiString, model.orderid);
            db.AddInParameter(dbCommand, "target", DbType.AnsiString, model.target);
            db.AddInParameter(dbCommand, "memo", DbType.AnsiString, model.memo);
            db.AddInParameter(dbCommand, "picture", DbType.AnsiString, model.picture);
            db.AddInParameter(dbCommand, "weishu", DbType.Int32, model.weishu);
            db.ExecuteNonQuery(dbCommand);
        }
    }
}

